package com.zhao.sparkstreaming.sgg.code

import scala.collection.mutable.ListBuffer
import scala.util.Random

/**
 * Description: <br/>
 * Copyright (c) ，2020 ， 赵 <br/>
 * This program is protected by copyright laws. <br/>
 * Date： 2020/12/4 14:14
 *
 * @author 柒柒
 * @version : 1.0
 */

case class RanOpt[T](value: T,weight: Int)

object RandomOptions {
    def apply[T](opts: RanOpt[T]*): RandomOptions[T] ={
      val randomOptions: RandomOptions[T] = new RandomOptions[T]()
      for (opt <- opts){
        randomOptions.totalWeight += opt.weight
        for (i <- 1 to opt.weight){
          randomOptions.optsBuffer += opt.value
        }
      }
      randomOptions
    }

}

class RandomOptions[T](opts: RanOpt[T]*){
  var totalWeight = 0
  var optsBuffer = new ListBuffer[T]

  def getRandomOpt : T ={
    val randomNum: Int = new Random().nextInt(totalWeight)
    optsBuffer(randomNum)
  }
}





